package com.yuandian.management.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.api.management.po.ConfMpChannelParams;
import com.yuandian.api.management.vo.ConfMpChannelExcelVo;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.api.management.entity.ConfMpChannel;
import com.yuandian.bpm.common.excel.annotation.RequestExcel;
import com.yuandian.bpm.common.excel.annotation.ResponseExcel;
import com.yuandian.bpm.common.excel.vo.ErrorMessage;
import com.yuandian.management.service.ConfMpChannelService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Collections;
import java.util.List;

/**
 * 中台监控-渠道配置表-Controller
 * @author zfsn
 * @date 2023-4-26
 */
@Api(tags = "中台监控-渠道配置")
@RestController
@RequestMapping("/mp/channel")
public class ConfMpChannelController {

	@Autowired
	private ConfMpChannelService confMpChannelService;

	/**
	 * 通过主键查询数据
	 * @param idList 主键集合
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "主键查询", notes = "通过主键查询渠道配置数据，支持批量查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "主键数组", paramType = "path", required = true, dataType = "String", dataTypeClass = String.class)
	})
	@GetMapping("/queryByIds/{ids}")
	// @PreAuthorize("@pms.hasPermission('conf_mp_channel_view')")
	public R<List<ConfMpChannel>> queryByIds(@PathVariable("ids") List<Integer> idList) {
		return R.ok(confMpChannelService.listByIds(idList));
	}

	/**
	 * 分页查询
	 * @param params 中台监控-渠道配置请求参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "current", value = "当前页", paramType = "body", required = true, dataType = "Long", dataTypeClass = Long.class),
			@ApiImplicitParam(name = "size", value = "每页条数", paramType = "body", required = true, dataType = "Long", dataTypeClass = Long.class),
			@ApiImplicitParam(name = "orders", value = "排序方式", paramType = "body", required = false, dataType = "OrderItem", dataTypeClass = OrderItem.class, allowMultiple = true),
			@ApiImplicitParam(name = "channelCodeList", value = "渠道编码集合", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class, allowMultiple = true),
			@ApiImplicitParam(name = "channelName", value = "渠道名称", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "internalOrgCodeList", value = "内部单位编码集合", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class, allowMultiple = true)
	})
	@PostMapping("/page")
	// @PreAuthorize("@pms.hasPermission('conf_mp_channel_view')")
	public R<Page<ConfMpChannel>> page(@ApiIgnore @RequestBody ConfMpChannelParams params) {
		return confMpChannelService.page(params);
	}

	/**
	 * 新增数据
	 * @param params 中台监控-渠道配置请求参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "新增数据", notes = "新增数据")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "channelCode", value = "渠道编码", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "channelName", value = "渠道名称", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "internalOrgCode", value = "内部单位编码", paramType = "body", required = true, dataType = "String", dataTypeClass = String.class)
	})
	@PostMapping("/save")
	// @PreAuthorize("@pms.hasPermission('conf_mp_channel_add')")
	public R<Boolean> save(@ApiIgnore @RequestBody ConfMpChannelParams params) {
		return confMpChannelService.saveWrap(params);
	}

	/**
	 * 通过主键更新数据
	 * @param params 中台监控-渠道配置请求参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "主键更新", notes = "通过主键更新渠道配置数据")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "主键", paramType = "body", required = true, dataType = "Integer", dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "channelCode", value = "渠道编码", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "channelName", value = "渠道名称", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class),
			@ApiImplicitParam(name = "internalOrgCode", value = "内部单位编码", paramType = "body", required = false, dataType = "String", dataTypeClass = String.class)
	})
	@PutMapping("/updateById")
	// @PreAuthorize("@pms.hasPermission('conf_mp_channel_update')")
	public R<Boolean> updateById(@ApiIgnore @RequestBody ConfMpChannelParams params) {
		return confMpChannelService.updateByIdWrap(params);
	}

	/**
	 * 通过主键删除数据
	 * @param idList 主键
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "主键删除", notes = "通过主键删除渠道配置数据，支持批量删除")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "主键数组", paramType = "path", required = true, dataType = "String", dataTypeClass = String.class)
	})
	@DeleteMapping("/removeByIds/{ids}")
	// @PreAuthorize("@pms.hasPermission('conf_mp_channel_delete')")
	public R<Boolean> removeByIds(@PathVariable("ids") List<Integer> idList){
		confMpChannelService.removeByIds(idList);
		return R.ok(Boolean.TRUE, "删除成功");
	}

	/**
	 * 通过Excel导入渠道配置
	 * @param excelVoList 中台监控-渠道配置Excel vo对象集合
	 * @param bindingResult 用来校验 实体 jsr303 校验失败结果
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "导入渠道", notes = "通过Excel导入渠道配置")
	@PostMapping("/import")
	public R<List<ErrorMessage>> importChannel(
			@RequestExcel(maxSheetNum = 1) List<ConfMpChannelExcelVo> excelVoList,
			BindingResult bindingResult) {
		return confMpChannelService.importChannel(excelVoList, bindingResult);
	}

	/**
	 * 下载渠道导入模板
	 * @return 模板excel文件流
	 */
	@ApiOperation(value = "下载渠道导入模板", notes = "下载渠道导入模板")
	@ResponseExcel
	@GetMapping("/downloadTemplate")
	public List<ConfMpChannelExcelVo> downloadChannelTemplate() {
		return Collections.singletonList(new ConfMpChannelExcelVo());
	}
}